<?php
namespace Admin\Controller;
use Think\Controller;
class AddressController extends BackController {
	/**
	 * 初始化
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * 列表
	 */
    public function index() {
		if($_GET['nickname']) {
			$nickname = I('get.nickname','','strip_tags');
			$where['nickname'] = array('like','%'.$nickname.'%');
		}
		/*
		$year = date('Y');
		$string = $year."0101";
		$yeartime = strtotime($string);
		$this->assign('yeartime',$yeartime);

		$string = ($year+1)."0101";
		$nextyeartime = strtotime($string);
		$this->assign('nextyeartime',$nextyeartime);
		 */
		$time = time();
		$pretime = $time-(60*60*24*365);
		$this->assign('pretime',$pretime);


		$pay = intval($_GET['pay']);
		if($pay == 1) {
			//$where['paytime'] = array('between',$yeartime.','.$nextyeartime);
			$where['paytime'] = array('gt',$pretime);
		}

		$data = D('address')->mselect2($where); 
		$this->assign('list',$data['list']);
		$this->assign('page',$data['page']);
		$this->display();
    }

	/**
	 * 添加、修改
	 */
	public function edit() {
		if(IS_POST) {
			$id = intval($_POST['id']);
			if(!empty($id)) {
				$news = M('address')->find($id);
				if(!empty($news['photo'])) {
					$photo = array_filter(explode('|',$news['photo']));
				} else {
					$photo = array();
				}

				if(!empty($news['desc'])) {
					$desc = json_decode($news['desc'],true);
				} else {
					$desc = array();
				}
				if(!empty($_FILES['fileimg']['name'])) {
				/****/
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize   =     10485760;// 设置附件上传大小
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
				// 上传单个文件 
				$info   =   $upload->uploadOne($_FILES['fileimg']);
				if(!$info) {// 上传错误提示错误信息
					//jssuccess($upload->getError(),U('Index/edithead'));
					$this->error($upload->getError());
					die();
				}else{// 上传成功 获取上传文件信息
					$photos = './Uploads/'.$info['savepath'].$info['savename'];

					$namearr = explode(".",$info['savename']);
					$namearr[0] = $namearr[0]."_".time();
					$names = implode(".",$namearr);

					$image = new \Think\Image(); 
					$image->open($photos);
					// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
					$image->thumb(650, 650)->save('./Uploads/'.$info['savepath'].$names);

					unlink('./Uploads/'.$info['savepath'].$info['savename']);
					$photos = '/Uploads/'.$info['savepath'].$names;
					$photo[] = $photos;
					$_POST['photo'] = implode("|",$photo);

					$desc[$photos] = strip_tags($_POST['desc']);
					$_POST['desc'] = json_encode($desc);

				}
				/****/
				}
			}
			$msg = D('Admin/address')->msave2();
			if($msg) {
				$this->success('操作成功！',U('Address/edit',array('id'=>$id)));
			} else {
				$this->error('操作失败！');
			}
			die();
		}
		$id = I('get.id','','intval');
		if(!empty($id)) {
			$info = D('address')->find($id);
			if(!empty($info['mobile'])) {
				$mobilearr = json_decode($info['mobile'],true);
				$info['mobilearr'] = $mobilearr;
			}
			$this->assign('info',$info);
			if(!empty($info['photo'])) {
				$info['photos'] = explode('|',$info['photo']);
			}
			$this->assign('photoarr',$info['photos']);

			if(!empty($info['desc'])) {
				$descarr = json_decode($info['desc'],true);
				$this->assign('descarr',$descarr);
			}

			$dqtime = $info['paytime']+(60*60*24*365);
			$this->assign('dqtime',$dqtime);
		} else {
			die('need id');
		}

		$time = time();
		$pretime = $time-(60*60*24*365);
		$this->assign('pretime',$pretime);
		$this->display();
	}

	/**
	 * 删除
	 */
	public function del() {
		$id = I('get.id','','intval');
		if(!empty($id)) {
			$msg = D('address')->delete($id);
			if(empty($msg)) {
				$this->error('操作失败！',U('Address/index'));
			} else {
				$this->success('操作成功！',U('Address/index'));
			}
		}
	}

	public function ajaxremoveimg() {
		$id = I('post.id','','intval');
		$info = D('address')->find($id);
		$key = I('post.key','','intval');
		$filename = I('post.filename','','strip_tags');
		$where['id'] = $id;
		if(!empty($info['photo'])) {
			$photo = array_filter(explode('|',$info['photo']));
		}
		unlink(".".$photo[$key]);
		unset($photo[$key]);
		/****/
		if(!empty($info['desc'])) {
			$descarr = json_decode($info['desc'],true);
		}
		unset($descarr[$filename]);
		$data['desc'] = json_encode($descarr);
		/****/
		$data['photo'] = implode("|",$photo);
		$msg = D('address')->where($where)->save($data);
		if($msg) {
			echo json_encode(array(
				'code' => 1,
			));
		} else {
			echo json_encode(array(
				'code' => 2,
			));
		}
	}

	public function tel() {
		Header ( "Content-Disposition: attachment; filename=11.txt");    
		$time = time();
		$pretime = $time-(60*60*24*365);
		$where['paytime'] = array('gt',$pretime);
		$data = D('address')->where($where)->select(); 
		foreach($data as $key=>$value) {
			if($value['mobile']) {
				$tel = json_decode($value['mobile']);
				foreach($tel as $k=>$v) {
					echo $value['nickname']."：".$v."\r\n";
				}
			}
		}
		
	}

}
